<?php
	class Character extends CI_Controller {
		public function index() {
			$data['title'] = 'Character';
			$data['logged'] = true;
			$data['loggedChar'] = true;
			$data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
			$data['page'] = 'character';
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			$this->load->view('character/index');
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function profile() {
			$data['title'] = 'Character Profile';
			$data['logged'] = true;
			$data['loggedChar'] = true;
			$data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
			$data['page'] = 'character';
			$data['character'] = $this->Character_model->getUserStats($this->session->userdata('charId'));
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			$this->load->view('character/profile');
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function add_stats() {
			// TODO add stats method
		}
		
		public function shop($id) {
			$data['title'] = 'Character Profile';
			$data['logged'] = true;
			$data['loggedChar'] = true;
			$data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
			$data['page'] = 'character';
			$data['character'] = $this->Character_model->getUserStats($this->session->userdata('charId'));
			$data['characterName'] = $data['character']['name'];
			$data['shopId'] = $id;
			$shop = $this->Character_model->getShopStats($id);
			$shopItems = $this->Character_model->getShopItems($shop['ID']);
			$data['shopName'] = $shop['name'];
			foreach($shopItems as $key => $i) {
				$item = $this->Character_model->getItemStats($i['item_id']);
				$shopItems[$key] = $item;
			}
			$inventory = $this->Character_model->getUserInventory($this->session->userdata('charId'));
			foreach($inventory as $key => $i) {
				$item = $this->Character_model->getItemStats($i['item_id']);
				$inventory[$key]['item_id'] = $item;
			}
			
			$data['shopItems'] = $shopItems;
			$data['inventory'] = $inventory;
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-w-js');
			$this->load->view('templates/menu');
			
			if($this->input->post()) {
				$itemId = $this->input->post('item-id');
				$action = $this->input->post('action');
				
				switch($action) {
					case 'buy':
						$itemStats = $this->Character_model->getItemStats($itemId);
						$buyValue = $itemStats['buyvalue'];
						echo $buyValue;
						break;
					case 'sell':
						break;
				}
				
				$this->load->view('character/shop');
			} else {
				$this->load->view('character/shop');
			}
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function bank() {
			// TODO bank method
		}
		
		public function equipment() {
			// TODO equipment method
		}
		
		public function explore() {
			// TODO explore method
		}
		
		public function logout() {
			$this->session->unset_userdata('charId');
			redirect('account/');
		}
		
		public function temple() {
			// TODO temple method
		}
		
		public function town() {
			$data['title'] = 'Character Profile';
			$data['logged'] = true;
			$data['loggedChar'] = true;
			$data['admin'] = $this->session->userdata('admin') == 1 ? true : false;
			$data['page'] = 'character';
			$character = $this->Character_model->getUserStats($this->session->userdata('charId'));
			$data['characterName'] = $character['name'];
			$data['shops'] = $this->Character_model->getAllShops();
			$zone = $this->Character_model->getZoneStats($character['zone_id']);
			$data['zone'] = $zone['name'];
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			$this->load->view('character/town');
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
	}
?>